<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/13
 * Time: 10:32
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Banner3 extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            $map['banner_part'] = 3;
            if(isset($keyword)&&$keyword!="")
            {
                $map['banner_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['banner_status'] = $province;
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['banner_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['banner_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['banner_time'] = ['< time', $end];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('banner')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('banner')
                ->where($map)
                ->page($NowPage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['banner_cate']==1){
                    $vo['banner_cate_name'] = '推荐购车';
                }elseif ($vo['banner_cate']==2){
                    $vo['banner_cate_name'] = '再次购车';
                }elseif ($vo['banner_cate']==3){
                    $vo['banner_cate_name'] = '新闻资讯';
                }else{
                    $vo['banner_cate_name'] = '';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $banner_title =input('banner_title');
            $banner_thumb = input('banner_thumb');
            $banner_type = input('banner_type');
            $banner_link = input('banner_link');
            $banner_cate= input('banner_cate');
            $banner_sort= input('banner_sort');
            $banner_new= input('banner_new');
            
            if($banner_type == 1){
                if(empty($banner_link)){
                    return json(['code' => 100, 'msg' => '请填写外部链接']);
                }
            }else{
                if($banner_cate == 3){
                    if(empty($banner_new)){
                        return json(['code' => 100, 'msg' => '请选择新闻资讯']);
                    }
                }
            }
            
            $data['banner_title'] = $banner_title;
            $data['banner_thumb'] = $banner_thumb;
            $data['banner_type'] = $banner_type;
            $data['banner_sort'] = $banner_sort;
            $data['banner_part'] = 3;
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            $data['banner_update_time'] = date('Y-m-d H:i:s',time());
            if($banner_type == 1){
                $data['banner_link'] = $banner_link;
            }
            if($banner_type == 2){
                $data['banner_cate'] = $banner_cate;
                if($banner_cate == 3){
                    $data['banner_object_id'] = $banner_new;
                }
            }
            
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $new = Db::table('up_new')->where('new_status', 1)->order('new_time desc')->select();
        $this->assign('new', $new);
        return $this->fetch();
    }
    
    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $banner_title =input('banner_title');
            $banner_thumb = input('banner_thumb');
            $banner_type = input('banner_type');
            $banner_link = input('banner_link');
            $banner_cate= input('banner_cate');
            $banner_new= input('banner_new');
            $data = [];
            if($banner_type == 1){
                if(empty($banner_link)){
                    return json(['code' => 100, 'msg' => '请填写外部链接']);
                }
                $data['banner_link'] = $banner_link;
                $data['banner_cate'] = '';
                $data['banner_object_id'] = '';
            }else{
                if($banner_cate == 3){
                    if(empty($banner_new)){
                        return json(['code' => 100, 'msg' => '请选择新闻资讯']);
                    }
                    $banner_object_id = $banner_new;
                }else{
                    $banner_object_id = '';
                }
                $data['banner_link'] = '';
                $data['banner_cate'] = $banner_cate;
                $data['banner_object_id'] = $banner_object_id;
            }
            
            $data['banner_title'] = $banner_title;
            $data['banner_type'] = $banner_type;
            $data['banner_update_time'] = date('Y-m-d H:i:s',time());
            if(!empty($banner_thumb)){
                $data['banner_thumb'] = $banner_thumb;
            }
            
            $res = Db::table('up_banner')->where('banner_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('banner_id');
        $info= Db::table('up_banner')->where('banner_id',$id)->find();
        // 新闻资讯
        $new = Db::table('up_new')->where('new_status', 1)->order('new_time desc')->select();
        $this->assign('id',$id);
        $this->assign('res',$info);
        $this->assign('new',$new);
        return $this->fetch();
    }
    
    /**
     * 修改排序
     * @return \think\response\Json
     */
    public function sort(){
        $param = input('');
        $res = Db::table('up_banner')->where('banner_id', $param['id'])->setField('banner_sort', $param['value']);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }
    
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('banner')->where('banner_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    
    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $count = Db::table('up_banner')
            ->where('banner_status',1)
            ->where('banner_part', 3)
            ->count();
        if($count>=5){
            return json(['code'=>100,'msg'=>'最多上架5个轮播图，请先下架其他轮播图再上架']);
        }
        $res = Db::name('banner')->where('banner_id',$task_id)->setField('banner_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'上架成功']);
        }else{
            return json(['code'=>100,'msg'=>'上架失败']);
        }
    }
    
    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('banner')->where('banner_id',$task_id)->setField('banner_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'下架成功']);
        }else{
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }
}